<template>
  <div style="margin-top: 60px;margin-left:330px;width: 300px;height: 500px;border: 0px solid red;" >
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="楼宇" prop="buildingId">
        <el-select v-model="ruleForm.buildingId" placeholder="请选择楼宇" @change="buildingChange">
          <el-option v-for="item in buildingList" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="宿舍" prop="dormitoryId">
        <el-select v-model="ruleForm.dormitoryId" placeholder="请选择宿舍" >
          <el-option v-for="item in dormitoryList" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分数" prop="score">
        <el-input v-model="ruleForm.score"></el-input>
      </el-form-item>
      <el-form-item label="日期" prop="createDate">
        <el-date-picker
            style="width: 200px"
            v-model="ruleForm.date"
            type="date"
            placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
export default {
  data() {
    return {
      category:null,
      ruleForm: {
        buildingId: '',
        dormitoryId: '',
        score:'',
        date: '',
        // dormitoryAdminId: ''
      },
      buildingList: '',
      dormitoryList: '',
      rules: {
        buildingId: [
          { required: true, message: '请选择楼宇', trigger: 'change' }
        ],
        dormitoryId: [
          { required: true, message: '请选择宿舍', trigger: 'change' }
        ],
        score: [
          { required: true, message: '请输入分数', trigger: 'change' }
        ],
        date: [
          { required: true, message: '请输入日期', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    buildingChange(){
      const _this = this
      axios.get('http://localhost:8181/clean/findDormitoryByBuildingId/'+_this.ruleForm.buildingId).then(function (resp) {
        _this.dormitoryList = resp.data.data
        _this.ruleForm.dormitoryId = ''
        // _this.ruleForm.studentId = ''
      })
    },
    // dormitoryChange(){
    //   const _this = this
    //   axios.get('http://localhost:8181/clean/findStudentByDormitoryId/'+_this.ruleForm.dormitoryId).then(function (resp) {
    //     _this.studentList = resp.data.data
    //     // _this.ruleForm.studentId = ''
    //   })
    // },
    submitForm(formName) {
      const _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let admin = _this.$store.state.admin
          _this.ruleForm.dormitoryAdminId = admin.id
          console.log(_this.ruleForm)
          axios.post('http://localhost:8181/clean/save',_this.ruleForm).then(function (resp) {
            if(resp.data.code == 0){
              _this.$alert('卫生登记成功', '', {
                confirmButtonText: '确定',
                callback: action => {
                  _this.$router.push('/cleanRecord2')
                }
              });
            }
          })
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    const _this = this
    axios.get('http://localhost:8181/absent/buildingList').then(function (resp) {
      _this.buildingList = resp.data.data
    })
  }
}
</script>
